package com.crm.dao;
import java.util.ArrayList;
import java.util.List;



import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import com.crm.entity.Clientdet;
import com.crm.entity.Sources;
import com.crm.entity.Supplier;
import com.crm.util.ApplicationContext;
public class SourcesDAO {
	protected final Log logger = LogFactory.getLog(getClass());
	
	public  int insert(Sources s) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			session.saveOrUpdate(s);
			tx.commit();
			return 0;
		}
		catch (Exception e) {
		if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  int update(Sources s) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			session.saveOrUpdate(s);
		tx.commit();
			return 0;
		}
		catch (Exception e) {
		if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  int delete(Sources s) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		tx = session.beginTransaction();
			session.delete(s);
			tx.commit();
			return 0;
		}
		catch (Exception e) {
	if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  List<Sources> findAll(int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			List<Sources> custObjs=new ArrayList<Sources>();
			List objs=null;
			Query query=session.createQuery("From Sources where is_deleted!=1 and business_id=:busid");
			query.setInteger("busid", busid);
			objs=query.list();
			for(Object o:objs){
				custObjs.add((Sources)o);
			}
			return custObjs;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
	public Sources findById(int i,int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			String qrystr="From Sources where id=:sid and business_id=:busid";
			Query qry=session.createQuery(qrystr);
			qry.setInteger("sid", i);
			qry.setInteger("busid", busid);
			Object ob=qry.uniqueResult();
			Sources s=(Sources)ob;
			return s;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
	public Sources getByName(String sname,int busid)throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			String querystr="From Sources where sourcename=:name and business_id=:busid";
			Query query=session.createQuery(querystr);
			query.setInteger("busid", busid);
			query.setString("name", sname);
			Object queryresult=query.uniqueResult();
			Sources s=(Sources)queryresult;
			return s;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
public  String sourceCheck(int busid,String sname) throws Exception {
	Session session =  ApplicationContext.getHiberSession().openSession();
	try {
		Query qry=session.createQuery("from Sources where business_id=:busid  and sourcename=:name");
		logger.info("the query is"+qry);
		qry.setString("name",sname);
		qry.setInteger("busid",busid);
		logger.info("Name In DAO"+sname);
		Object obj=qry.uniqueResult();
		Sources c=(Sources)obj;
		try{
			if(c.getSourceName()==null|| c.getSourceName()==""){
			return "true";
			}
		}catch(NullPointerException e){
			logger.info("the client name is In Null pointer+");
			return "true";
		}
		logger.info("the client name is +");
		return "false";
	}
	catch (Exception e) {
		throw e;
	}
	finally {
		session.close();
	}
	}
	public  void deleteSource(int sourceid,int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			String qtr="Update Sources set is_deleted=1 where id=:id and business_id=:busid";
			Query qry=session.createQuery(qtr);
			qry.setInteger("id", sourceid);	
			qry.setInteger("busid", busid);
			qry.executeUpdate();
			tx.commit();
			return ;
		}
		catch (Exception e) {
		if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
/*	public Sources getSrcId(String c,int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			String querystr="from Sources where sourcename=:source and business_id=:busid";
			Query qury=session.createQuery(querystr);
			qury.setString("source", c);
			qury.setInteger("busid", busid);
			logger.info("source "+c);
			Object queryresult=qury.uniqueResult();
			Sources s=(Sources)queryresult;
			return s;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}*/

}
